<?php
class Admin_ApplyController extends Ctrl_Admin{
        
        public function indexAction(){
            $q = $_REQUEST;
            $tACMD = new ApplycompModel;
            $tCMDatas = $tACMD->field('*')->fList();
            $this->assign('tCMDatas',$tCMDatas);
        }

        /*
         *添加配货列表
         */
        public function addlistAction(){
            $q        = $_REQUEST;
            $userinfo = $_SESSION['admin'];
            $userid   = $userinfo['id'];
            $time     = time();
            $lModel   = new ApplylistModel;
            $tAGMD 	  = new ApplygoodModel;
            $tDPMD    = new DispatchingModel;
            $tPURCHMO = new PurchasesModel;
            $tAMD = new AdminModel;
            $Applyname= $q['applyname'];
            $Applycom = $q['applycom'];
            $Applynum = $q['applynum'];
            $nanum = count(array_filter($Applyname));
            $conum = count(array_filter($Applyname));
            $nunum = count(array_filter($Applyname));
            $newArray="";//处理成添加数组
 			$num = count($Applyname);
            $WMD = new WorkerModel;
            $id = $_SESSION['admin']['id'];
            // $departmet = filed('num')->where('introduce=总部')->fRow();
            $tSql = 'select w.department from '.$tAMD->table.' a left join '.$WMD->table.' w on a.w_id=w.id where a.id='.$id;
            $department = $tAMD->query($tSql);
            // print_r($department);die;
            if ($num!=$nanum||$num!=$conum||$num!=$nunum) {
                Tool_Fnc::ajaxMsg('请填写完信息在提交','0');die;
            }
 			for ($i=0; $i < $num ; $i++) { 
 				$tAGDatas = $tAGMD->field('good_name')->where('good_name="'.$Applyname[$i].'"')->fList();
 				if (empty($tAGDatas)) {
 					$AGDatas = array(
 						'good_name' => $Applyname[$i],
 						);
 					$tAGMD->insert($AGDatas);
 				}
 				$LMDatas = array(
 					'id' => $userid,
 					'apply_time' => $time,
 					'apply_com' => $Applycom[$i],
 					'apply_name' => $Applyname[$i],
 					'apply_num' => $Applynum[$i],
 					'apply_bei' => '无',
 					'sta_id' => '1',
                    'department' => $department[0]['department'],
 				);
 				if(!$lModel->insert($LMDatas)){
 					Tool_Fnc::ajaxMsg('提交失败','0');die;
 				}else{
                    $tSql = 'select max(apply_id) from '.$lModel->table;
                    $id = $lModel->query($tSql);
                    $apData = array(
                    'a_id' => $id[0]['max(apply_id)'],
                    'status' => '1',
                    'created' => $time,
                    );
                    if (!$tPURCHMO->insert($apData)) {
                        Tool_Fnc::ajaxMsg('提交失败','0');die;
                    }
                    
                }
 			}
 			Tool_Fnc::ajaxMsg('提交成功','1');die;

        }

        /*
         *
         * 配货列表
         */
        public function listAction(){
            $q = $_REQUEST;
            $statusid = empty($q['statusid'])? "" : Tool_Fnc::safe_string($q['statusid']);
            $department_id = empty($q['department_id'])? "" : Tool_Fnc::safe_string($q['department_id']);
            $statusStr = "";
            $departmentStr = "";
            if($department_id){//department选择不为空
                $departmentStr = ' and department_info.num='.$department_id;
            }
            if($statusid){//status选择不为空
                $statusStr = ' and apply_list.sta_id='.$statusid;
            }
            $lModel   = new ApplylistModel;
            $dModel   = new DepartmentinfoModel;
            $userinfo = $_SESSION['admin'];
            $idStr    = " ";
            if($userinfo['id']!=1){//不是超级管理员登录
                $idStr = ' and apply_list.id ='.$userinfo['id'].' ';
            }
            $tSql= "select count(apply_list.apply_id) c from apply_list 
                    left join admin on apply_list.id=admin.id 
                    left join status_type on apply_list.sta_id=status_type.sta_id 
                    left join department_info on apply_list.department = department_info.num where 1 ".$idStr.$departmentStr.$statusStr;
            $tCount = $lModel->query($tSql);
            $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
            $tLimit = 10;
            $tPage = new Tool_Page($tCnt,$tLimit);
            $tSql= "select apply_list.*,department_info.introduce,admin.name,status_type.sta_name from apply_list 
                    left join admin on apply_list.id=admin.id 
                    left join status_type on apply_list.sta_id=status_type.sta_id 
                    left join department_info on apply_list.department = department_info.num where 1".$idStr.$departmentStr.$statusStr.' order by apply_id desc limit '.$tPage->limit();
            $listinfo = $lModel->query($tSql);
            $departmentinfo = $dModel->field('introduce,num')->fList();
            foreach ($listinfo as $key => $value) {
            	$listinfo[$key]['apply_time'] = date('Y-m-d,H:i:s',$value['apply_time']);
            }
            $this->assign('listinfo',$listinfo);
            $this->assign('statusid',$statusid);
            $this->assign('department_id',$department_id);
            $this->assign('departmentinfo',$departmentinfo);
            $this->assign('pageinfo',$tPage->show());
        }
            
        /*
         *设备列表
         */
        public function reportAction(){
            $q = $_REQUEST;
            $statusid = empty($q['statusid'])? "" : Tool_Fnc::safe_string($q['statusid']);
            $Department = empty($q['department_id'])?'':Tool_Fnc::safe_string($q['department_id']);
            $tDIMD = new DepartmentinfoModel;
            $tARMO = new ApplyrepModel;
            $tAMD = new AdminModel;
            $WMD = new WorkerModel;
            $tWhere = "1";
            if(!empty($Department)){//department选择不为空
                $tWhere .= ' and departmnt ='.$Department;
            }
            if(!empty($statusid)){//status选择不为空
                $tWhere .= ' and rep_status='.$statusid;
            }
            if($_SESSION['admin']['id']!=1){
                $tsql = 'select department from '.$tAMD->table.' where id='.$_SESSION['admin']['id'];
                $tAData = $tAMD->query($tsql);
                $tWhere .= ' and departmnt = '.$tAData[0]['department'];
            }	

            $tSql = 'select count(*) c from '.$tARMO->table.' where '.$tWhere;
        	$tCount = $tARMO->query($tSql);
        	$tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
	        $tLimit = 10;
	        $tPage = new Tool_Page($tCnt,$tLimit);
	        $tDatas = array();
	        if(!empty($tCnt)){ 
            $tSql = 'select a.name,rep_id,r.id,rep_name,rep_type,rep_time,rep_rea,rep_status,rep_sapre,departmnt from  '.$tARMO->table.' r left join '.$tAMD->table.' a on r.id=a.id where '.$tWhere.' order by rep_id desc limit '.$tPage->limit();
            $tDatas = $tARMO->query($tSql);
                foreach($tDatas as $tK=>$tV){
                $tFData = $tDIMD->field('num,introduce')->where('num = '.$tV['departmnt'])->fRow();
                $tDatas[$tK]['departmnt'] = $tFData['introduce'];
                $tDatas[$tK]['rep_time'] = date('Y-m-d,H:i:s',$tV['rep_time']);
            	}
        	}
            $id = $_SESSION['admin']['id'];
            $tSql = 'select w.department from '.$tAMD->table.' a left join '.$WMD->table.' w on a.w_id=w.id where a.id='.$id;
            $department = $tAMD->query($tSql);
            $tDIDatas = $tDIMD->field('num,introduce')->fList();
            $this->assign('tDatas',$tDatas);
       	 	$this->assign('tShow',$tPage->show());
            $this->assign('tDIDatas',$tDIDatas);
            $this->assign('Department',$Department);
            $this->assign('statusid',$statusid);
        }
        /*
         *报备添加
         */
        public function reportaddAction(){
        	$q = $_REQUEST;
        	$Reptype = empty($q['reptype'])?Tool_Fnc::ajaxMsg('类型不能为空'):Tool_Fnc::safe_string($q['reptype']);
        	$Repname = empty($q['repname'])?Tool_Fnc::ajaxMsg('名称不能为空'):Tool_Fnc::safe_string($q['repname']);
        	$Reprea = empty($q['reptrea'])?'':Tool_Fnc::safe_string($q['reptrea']);
            $Repnum = empty($q['repnum'])?Tool_Fnc::ajaxMsg('数量不能为空'):Tool_Fnc::safe_string($q['repnum']);
        	$tRRMD = new ApplyrepModel;
        	$tAMD = new AdminModel;
            $apModel = new ApparatusModel;
            $apEQMO = new ApplyequiModel;
            $DIMD = new DepartmentinfoModel;
            $WMD = new WorkerModel;
            $id = $_SESSION['admin']['id'];
            // $departmet = filed('num')->where('introduce=总部')->fRow();
            $tSql = 'select w.department from '.$tAMD->table.' a left join '.$WMD->table.' w on a.w_id=w.id where a.id='.$id;
            $department = $tAMD->query($tSql);
            // Tool_Fnc::ajaxMsg('提交失败','0',$department);
        	$time = time();
            $tAGDatas = $apEQMO->field('equi_name')->where('equi_name="'.$Repname.'"')->fList();
            if (empty($tAGDatas)) {
                $AGDatas = array(
                    'equi_name' => $Repname,
                    );
                $apEQMO->insert($AGDatas);
            }
        	$ARData = array(
        		'rep_name' => $Repname,
        		'rep_rea' => $Reprea,
        		'rep_type' => $Reptype,
        		'rep_time' => $time,
        		'rep_status' => '1',
        		'id' => $id,
                'rep_sapre' => $Repnum,
        		'departmnt' => $department[0]['department'],
        		);
            $tRRMD->begin();
        	if (!$tRRMD->insert($ARData)) {
        		Tool_Fnc::ajaxMsg('提交失败','0');
        	}else{
                $tSql = 'select max(rep_id) from '.$tRRMD->table;
                $id = $tRRMD->query($tSql);
                $apData = array(
                    'r_id' => $id[0]['max(rep_id)'],
                    'status' => '1',
                    'created' => $time,
                    'department' => $department[0]['department'],
                    );
                if (!$apModel->insert($apData)) {
                    $tRRMD->back();
                   Tool_Fnc::ajaxMsg('提交失败','0');
                }
                $tRRMD->commit();
                Tool_Fnc::ajaxMsg('提交成功','1');
            }

        }
        /*
         *报备列表
         */
/*        public function reportlistAction(){
            $lModel = new ApplylistModel;
            $userinfo = $_SESSION['admin'];
            $listinfo = $lModel->field('*')->where('id='.$userinfo['id'])->fList;
            $this->assign('listinfo',$listinfo[0]);

        }*/

        /*
         *修改status
         */
        public function updatestatusAction(){
            $q = $_REQUEST;
            $statusid['apply_id'] = empty($q['id'])? "" : Tool_Fnc::safe_string($q['id']);
            $aModel = new ApplylistModel;
            $statusid['sta_id'] = 2;
            $result = $aModel->update($statusid);           
            if($result){
                Tool_Fnc::ajaxMsg('',1);die;
            }else{
                Tool_Fnc::ajaxMsg('修改失败',2);die;  
            }
        }
        public function upstatusAction(){
            $q = $_REQUEST;
            $arModel = new ApplyrepModel;
            $apModel = new ApparatusModel;
            $time = time();
            $statusid['rep_id'] = empty($q['id'])? "" : Tool_Fnc::safe_string($q['id']);
            $statusid['rep_status'] = 3;
            $result = $arModel->update($statusid);
            $id = $apModel->field('id')->where('r_id='.$statusid['rep_id'])->fList();
            $ap['updated'] = $time;
            $ap['status'] = 3;
            $ap['r_id'] = $statusid['rep_id'];
            $ap['id'] = $id[0]['id'];
            $res = $apModel->update($ap);
            if ($res) {
               Tool_Fnc::ajaxMsg('',1);die;
            }else{
                Tool_Fnc::ajaxMsg('修改失败',2);die;  
            }
        }
        /*
         *确认收货
         */
        public function conreceiptAction(){
            $q = $_REQUEST;
            $statusid['apply_id'] = empty($q['appid'])? "" : intval($q['appid']);
            $statusid['apply_bei'] = empty($q['appbei'])? "" : Tool_Fnc::safe_string($q['appbei']);
            $appModel = new ApplylistModel;
            $bei = $appModel->where('apply_id='.$statusid['apply_id'])->fOne('apply_bei');

            if($bei!='无'){
                Tool_Fnc::ajaxMsg('已提交请勿重复提交',2);die;
            }
            $statusid['sta_id']=5;
            $result = $appModel->update($statusid);
            if($result){
                Tool_Fnc::ajaxMsg('提价成功',1);die;
            }else{
                Tool_Fnc::ajaxMsg('修改失败',2);die;
            }


        }
        /*
         *配餐名称
         */
/*        public function applynameAction(){
        	$q = $_REQUEST;
        	$Applyname = empty($q['applyname'])?'':Tool_Fnc::safe_string($q['applyname']);
        	$tAGMD = new ApplygoodModel;
        	$tAGDatas = $tAGMD->field('good_name')->where('good_name like \'%'.$Applyname.'%\'')->fList();
        	$AGDatas = json_encode($tAGDatas);
        	Tool_Fnc::ajaxMsg('',1,"$AGDatas");die;

        }*/

}
